package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.Order;
import com.runfast.paotui.dao.model.RunfastCuseraddress;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.OrderService;
import com.runfast.paotui.service.RunfastCuseraddressService;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.waimai.dao.model.RunfastBusiness;
import com.runfast.waimai.dao.model.RunfastBusinessComment;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.service.RunfastBusinessCommentService;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/shopper/all/*")
public class ShopperAllController extends DriverBaseController {

    @Resource
    RunfastShopperService shopperService;
    @Resource
    RunfastGoodsSellRecordService runfastGoodsSellRecordService;
    @Resource
    RunfastBusinessCommentService runfastBusinessCommentService;
    @Resource
    OrderService orderService;
    @Resource
    RunfastBusinessService runfastBusinessService;
    @Resource
    RunfastCuseraddressService runfastCuseraddressService;
    //按照 起始时间查询 代理商的 骑手信息
    @PostMapping("driver")
    public @ResponseBody ModelMap driver(
            Integer agereid ,String name ,String createtime1, String createtime2
    ) throws ParseException {
        ModelMap model=new ModelMap();
        String name1="%"+name+"%";
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time1=format.parse(createtime1.toString());
        Date time2=format.parse(createtime2.toString());
        List<RunfastShopper> runfastShopperList =shopperService.findNameAndAgerenAndTime(1,name1,time1,time2);
        if(runfastShopperList==null){
                model.put("msg","没有符合条件的统计");
        }model.put("list",runfastShopperList);
        return  model;
    }
    /* 历史定单统计*/
    @PostMapping("orderanddriver")
    public @ResponseBody ModelMap orderanddriver(String token,  String startTime, String endTime, Integer a,
                                                 @RequestParam(value = "page",required = false,defaultValue = "0")Integer page,
                                                 @RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize){
        /*a=1 为跑腿 a=2为外卖*/
        ModelMap model=new ModelMap();
        DateFormat format=new SimpleDateFormat("yyyy-MM-ddHH-mm-ss");
        Integer page1=page * pagesize;
        RunfastShopper runfastShopper=super.getLoginShopper(token);

        try {
            Date time1=format.parse(startTime+"00-00-00");
            Date time2=format.parse(endTime+"23-59-59");
            System.out.println(time1+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
            System.out.println(time2+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

            if(null==runfastShopper){
                model.put("success",false);
                model.put("msg","没有上线");
            }else {
                if(a==1){
                    List<Order>order=orderService.findAllAndTime(runfastShopper.getId(),page1,pagesize,time1,time2);//去掉了分页
                    Integer count=orderService.findAllAndTimecount(runfastShopper.getId(),page1,pagesize,time1,time2);
                    System.out.println(count+"##################");
                    List<Order>list=new ArrayList<Order>();
                    List<Order>list1=new ArrayList<Order>();
                                for(Order s:order){
                                     RunfastCuseraddress runfastCuseraddress=runfastCuseraddressService.findById(s.getToId());
                                    Order order1=new Order();
                                    order1.setToAddress(runfastCuseraddress.getUserAddress()+runfastCuseraddress.getAddress());
                                    order1.setToName(runfastCuseraddress.getName());
                                    order1.setToMobile(runfastCuseraddress.getPhone());
                                      order1.setTimeCompleted(s.getTimeCompleted());
                                      list.add(order1);

                                }
                    System.out.println(order.size()+"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                    if(list == null){//当传入过来的list集合为null时，先进行实例化
                       list = new ArrayList<Order>();
                    }
                    Integer pageNo=page+1;
                    if((Object)pageNo == null){//当传入过来的pageNo为null时，先进行赋值操作
                        pageNo = 1;
                    }
                    if((Object)pagesize == null){//当传入过来的dataSize为null时，先进行赋值操作
                        pagesize = 1;
                    }
                    if(pageNo <= 0){
                        pageNo = 1;
                    }
                    //记录一下数据一共有多少条
                    int totalitems = list.size();
                //实例化一个接受分页处理之后的数据
            //        List<Order> afterList = new ArrayList<Order>();
                    for
                            ( int i = (pageNo-1)*pagesize;
                              i < (((pageNo -1)*pagesize) + pagesize >
                                      totalitems ? totalitems:((pageNo -1)*pagesize) +pagesize);
                              i++) {
//然后将数据存入afterList中

                        list1.add(list.get(i));
                    }
//                    //********************************************************
//                                if(list.size()<pagesize){
//                                    for(int i=page1;i<(page1+list.size()%pagesize);i++) {
//
//                                        list1.add(list.get(i));
//                                    } }else {
//                                for(int i=page1;i<(page1+pagesize);i++){
//                                        if(null !=list.get(i)){
//                                            list1.add(list.get(i));
//                                        }
//                                   }
//                    }

                    if(order.size()>0&&count!=null){
                        System.out.println("aaaaaa"+list);
                     model.put("order",list1);
                        model.put("count",count);
                        model.put("success",true);
                    }else {
                        model.put("success",false);
                        model.put("msg","没有数据统计了");

                    }

                }
               else if(a==2){
                    Integer pageNo=page+1;
                    List<RunfastGoodsSellRecord>list=runfastGoodsSellRecordService.findAllAndTime(runfastShopper.getId(),page1,pagesize,time1,time2);//这里不能加分页，不然只能显示分页的数量
                    Integer count1=runfastGoodsSellRecordService.findAllAndTimecount(runfastShopper.getId(),page1,pagesize,time1,time2);

                            Set<RunfastBusiness> set=new HashSet<RunfastBusiness>();
                             List<RunfastBusiness> set1=new ArrayList<RunfastBusiness>();
                    List<RunfastBusiness> list1=new ArrayList<RunfastBusiness>();
                             HashSet<Integer> setid=new HashSet<Integer>();


                    for(RunfastGoodsSellRecord s:list){
                                       setid.add(s.getBusinessid());  //去掉重复的

     }

                        for(Integer sid:setid){
                            RunfastBusiness runfastBusiness=runfastBusinessService.findById(sid);

                            Integer businesscount=runfastGoodsSellRecordService.findNameAndCount(runfastShopper.getId(),page1,pagesize,time1,time2,runfastBusiness.getName());

                            RunfastBusiness runfastBusiness1=new RunfastBusiness();

                            runfastBusiness1.setCode(runfastBusiness.getCode());
                            runfastBusiness1.setName(runfastBusiness.getName());
                            runfastBusiness1.setMiniImgpath(runfastBusiness.getMiniImgpath());//店铺log缩小图
                            runfastBusiness1.setScore(runfastBusiness.getScore());//店铺评星
                            runfastBusiness1.setAddress(runfastBusiness.getAddress());
                            runfastBusiness1.setLevelid(runfastBusiness.getLevelid());
                            runfastBusiness1.setBusinessCount(businesscount);
                            set.add(runfastBusiness1);
                        }
                    System.out.println(set.size()+"***************************************");

                        for(RunfastBusiness runfastBusiness:set){
                        set1.add(runfastBusiness);
                        }
                    System.out.println(set1.size()+"****************************************");

                    if(list == null){//当传入过来的list集合为null时，先进行实例化
                        list = new ArrayList<RunfastGoodsSellRecord>();
                    }
                    if((Object)pageNo == null){//当传入过来的pageNo为null时，先进行赋值操作
                        pageNo = 1;
                    }
                    if((Object)pagesize == null){//当传入过来的dataSize为null时，先进行赋值操作
                        pagesize = 10;
                    }
                    if(pageNo <= 0){
                        pageNo = 1;
                    }
                    //记录一下数据一共有多少条
                    int totalitems = set1.size();
                    //实例化一个接受分页处理之后的数据
                    //        List<Order> afterList = new ArrayList<Order>();
                    System.out.println(set1.size()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
                    for
                            ( int i = (pageNo-1)*pagesize;
                              i < (((pageNo -1)*pagesize) + pagesize > totalitems ? totalitems:((pageNo -1)*pagesize) +pagesize);
                              i++) {


                        list1.add(set1.get(i));
                    }




                    System.out.println(list1+"#####################################################");
                    if(set1.size()>0&&count1!=null){
                         model.put("list",list1);
                        model.put("count",count1);
                        model.put("success",true);
                    }else {
                        model.put("success",false);
                        model.put("msg","没有数据统计了");
                    }

                }else {
                    model.put("success",false);
                    model.put("msg","系统错误");
                }
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  model;
    }
}
